﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for PagingMaker
/// </summary>
public class PagingMaker<T>
{
	public PagingMaker(int pageSize)
	{
        this.pageSize = pageSize;
	}

    public int pageSize{set;get;}

    public int startRowNumber { set; get; }

    public int endRowNumber { set; get; }

    public int currentPage { set; get; }

    public int totalRowCount { set; get; }

    public int totalPages { set; get; }

    public List<T> getPageDate(List<T> data, int pageNumber)
    {
        if (data == null || data.Count==0)
        {
            startRowNumber = 0;
            endRowNumber = 0;
            currentPage = 0;
            totalRowCount = 0;
            totalPages = 0;

            return new List<T>();
        }

        totalPages = (data.Count - 1) / pageSize + 1;
        if (pageNumber > totalPages) pageNumber = totalPages;
        totalRowCount = data.Count;

        this.currentPage = pageNumber;
        startRowNumber = pageSize * (pageNumber - 1) + 1;
        endRowNumber = pageSize * pageNumber > data.Count ? data.Count : pageSize * pageNumber;

        List<T> currentPageData = new List<T>();
        for (int i = startRowNumber-1; i < endRowNumber; i++)
        {
            currentPageData.Add(data.ElementAt(i));
        }

        return currentPageData;
    }

}